package com.itboye.mengwu.data.repository;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.core.utils.BaseErrorListener;
import com.itboye.core.utils.BaseSuccessReqListener;
import com.itboye.core.utils.ByJsonRequest.Builder;
import com.itboye.mengwu.data.interfaces.IRedBag;
import com.itboye.mengwu.data.model.GoodsAskQueryModel;
import com.itboye.mengwu.data.model.RedBagModel;

public class RedBagRepository extends BaseNetRepository implements
		IRedBag<RedBagModel> {
	static final String REDBAG = "BY_RedEnvelope_query"; // 9.1 红包查询不分页
	static final String YOUHUIQUAN = "BY_Coupon_query"; // 9.1 红包查询不分页
	static final String FreightCost = "BY_Freight_cost";// 查询运费
	static final String FreightCostGoods = "BY_Freight_costSku";// 立即购买的运费
	String api = "101";

	public RedBagRepository(ICompleteListener listener) {
		super(listener);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void add(RedBagModel model) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(RedBagModel model) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(RedBagModel model) {
		// TODO Auto-generated method stub

	}

	@Override
	public void getInfo(RedBagModel model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<ArrayList<RedBagModel>>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", model.getUid());
		if (!model.getUse_status().equals("2"))
			params.put("is_use", model.getUse_status());// 已使用1,未使用0 默认全部
		else
			params.put("is_expire", "1");// 红包过期状态
		// 目前最简化调用
		// START
		(new Builder<ArrayList<RedBagModel>>())
				.setTypeVerParamsAndReturnClass(REDBAG, apiVer, params, type)
				.requestListener(
						new BaseSuccessReqListener<ArrayList<RedBagModel>>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public List<RedBagModel> query(RedBagModel model) {
		Type type = new TypeToken<ArrayList<RedBagModel>>() {
		}.getType();
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", model.getUid());
		if(!model.getUse_status().equals("2"))
		params.put("is_use", model.getUse_status());// 已使用1,未使用0 默认全部
		else
		params.put("is_expire", "1");
		// 目前最简化调用
		// START
		(new Builder<ArrayList<RedBagModel>>())
				.setTypeVerParamsAndReturnClass(YOUHUIQUAN, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<ArrayList<RedBagModel>>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
		return null;
	}

	@Override
	public void caculateFreightCost(String uid, String addressId, String cartIds) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		params.put("address_id", addressId);//
		params.put("cart_ids", cartIds);//
		// 目前最简化调用
		// START
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(FreightCost, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void caculateFreightCost(String uid, String addressId, String pid,
			String count) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		params.put("address_id", addressId);//
		params.put("pid", pid);//
		params.put("count", count);
		// 目前最简化调用
		// START
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(FreightCostGoods, apiVer,
						params, type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

}
